#!/bin/sh
if [ "$EUID" = "0" ] || [ "$USER" = "root" ]; then
	echo "Installing USBCID Driver..."
else
	echo "You do not have enough permission to install driver."
	echo "Run installer in root permission."
	echo "Installation canceled."
	exit 0
fi

DIR=/lib/modules/`uname -r`/usb
if [ ! -d $DIR ]; then
	mkdir $DIR
fi

if [ $? -ne 0 ]; then
	echo "Error: path '$DIR' does not exist."
	exit 1
fi

TEST=`lsmod | grep usbtest`
if [ -n "$TEST" ]; then
	rmmod usbtest
fi

cp *.ko $DIR/.
if [ $? -ne 0 ]; then
	echo "Fatal Error: Can't copy driver files"
	exit 1
fi

depmod
if [ $? -ne 0 ]; then
	echo "Error: command 'depmod' failed."
	exit 1
fi

groupadd -f usbcid

if [ ! -d /etc/udev/rules.d ]; then
	echo "Warning: It seems like that udev is not running on this machine."
	echo "It is recommended that you install the udev."
	
	DEV_NAME=cid
	DEV_NUM=0
	MINOR=160
	
	while [ $DEV_NUM -lt 16 ]
	do
		FILENAME=/dev/$DEV_NAME$DEV_NUM
		mknod $FILENAME c 180 $MINOR
		chown root:usbcid $FILENAME
		let DEV_NUM=DEV_NUM+1
		let MINOR=MINOR+1
	done
else
	cp usbcid.rules /etc/udev/rules.d/.
	if [ $? -ne 0 ]; then
		echo "Error: install failed - usbcid.rules"
		exit 1
	fi
fi

echo "Installation completed!"

